﻿/****************************************************************
*   作者：qq918
*   创建时间：2017/12/20 13:37:32
*   描述说明：

    构建帮助类
*****************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Model;

namespace MyEditor
{
    public static class BuildHelper
    {

        public static void GenerateVersionInfo(string dir)
        {
            if (!Directory.Exists(dir))
            {
                return;
            }

            // 原型
            VersionConfig versionProto = new VersionConfig();

            GenerateVersionProto(dir, versionProto, "");

            string json = versionProto.ToJson();

            using (FileStream fs = new FileStream($"{dir}/Version.json", FileMode.Create))
            {
                byte[] bytes = Encoding.UTF8.GetBytes(json);

                fs.Write(bytes, 0, bytes.Length);
            }
        }


        public static void GenerateVersionProto(string dir ,VersionConfig versionConfig,string relativePath)
        {
            foreach (string file in Directory.GetFiles(dir))
            {
                // 忽略清单文件
                if(file.EndsWith(".manifest") || file.EndsWith(".meta"))
                {
                    continue;
                }

                // 
                string md5 = MD5Helper.FileMD5(file);

                FileInfo fileInfo = new FileInfo(file);

                long size = fileInfo.Length;

                string filePath = relativePath == "" ? fileInfo.Name : $"{relativePath}/{fileInfo.Name}";

                versionConfig.FileVersionInfos.Add(new FileVersionInfo
                {
                    File = filePath,
                    MD5 = md5,
                    Size = size
                });
            }

            foreach (string director in Directory.GetDirectories(dir))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(director);
                string rel = relativePath == "" ? directoryInfo.Name : $"{relativePath}/{directoryInfo.Name}";
                GenerateVersionProto(director, versionConfig, rel);
            }
        }
    }
}
